var Resource = _p.Loadable.extend(
/**
 * Describes the Resource class - a parent for all entities which have resourceUri and guid
 * @lends Resource
 * @property {String} resourceUri
 * @property {String} guid
 */
{
    defaults: {
        resourceUri: null,
        guid: null
    },

    construct: function(data) {
        if ("string" == typeof(data)) {
            data = {
                resourceUri: data
            }
        }
        this._superMethod.apply(this, [data]);
    },

    extractGUID: function() {
        var rz = /\/([a-f0-9]{32})\/(\?[^\?]*)?$/i.exec(this.data.resourceUri);
        return (rz && 3 == rz.length) ? rz[1] : null;
    },

    satelliteClasses: {
        Proxy:
        /** @lends ResourceProxy*/
        {
            createResourceUri: "/api/",

            /**
             * Detects current loading status basing on the model's data
             */
            detectState: function() {
                return (this.modelData.resourceUri && !this.modelData.guid) ? this.STATES.EMPTY : this.STATES.COMPLETE;
            }
        },

        ProxyWeb: {
            /**
             * Returns appropriate loading key for the model
             */
            getLoadingKey: function() {
                return {
                    url: this.modelData.resourceUri
                };
            },

            /**
             * Returns appropriate saving key for the model
             */
            getSavingKey: function() {
                return {
                    url: this.modelData.resourceUri,
                    type: this.modelData.guid? "PUT" : "POST"
                }
            },

            getSavingPreparedData: function() {
                return {
                    data: JSON.stringify(this._superCall())
                }
            }
        },

        View: {
            loadingTemplate: $("#template .loading-resource"),
            errorTemplate: $("#template .loading-resource-error"),

            render: function() {
                var self = this;
                this._superCall();
                $("button.load", this.errorTemplate).click(function(event) {
                    self.getProxy().load();
                });
            }
        }
    }
});
